package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.DrawPrizeParam;
import com.example.lotterysystem.controller.param.ShowWinningRecordsParam;
import com.example.lotterysystem.controller.result.WinningRecordsResult;
import com.example.lotterysystem.service.IDrawPrizeService;
import com.example.lotterysystem.service.dto.WinningRecordDTO;
import com.rabbitmq.client.Command;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class DrawPrizeController {
    @Autowired
    private IDrawPrizeService drawPrizeService;

    @RequestMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(@RequestBody @Validated DrawPrizeParam param) {
        log.info("drawPrize param:{}", JacksonUtil.writeValueAsString(param));
        drawPrizeService.drawPrize(param);
        return CommonResult.success(true);
    }

    @RequestMapping("/winning-records/show")
    public CommonResult<List<WinningRecordsResult>> showWinningRecords(
            @RequestBody @Validated ShowWinningRecordsParam param){
        log.info("showWinningRecords param:{}", JacksonUtil.writeValueAsString(param));
        List<WinningRecordDTO> winningRecordDTOList = drawPrizeService.getRecords(param);
        return CommonResult.success(convertToWinningRecordsResultList(winningRecordDTOList));
    }

    private List<WinningRecordsResult> convertToWinningRecordsResultList(List<WinningRecordDTO> winningRecordDTOList) {
        if(CollectionUtils.isEmpty(winningRecordDTOList)){
            return Arrays.asList();
        }
        return winningRecordDTOList.stream().map(winningRecordDTO -> {
            WinningRecordsResult result = new WinningRecordsResult();
            result.setWinnerId(winningRecordDTO.getWinnerId());
            result.setWinnerName(winningRecordDTO.getWinnerName());
            result.setPrizeName(winningRecordDTO.getPrizeName());
            result.setPrizeTier(winningRecordDTO.getPrizeTier().getMessage());
            result.setWinningTime(winningRecordDTO.getWinningTime());
            return result;
        }).collect(Collectors.toList());
    }
}
